<template>
  <el-drawer
    v-model="showDrawer"
    :title="title"
    :size="size"
    :close-on-click-modal="false"
    :destroy-on-close="destroyOnClose"
  >
    <div class="formDrawer">
      <div class="body">
        <slot> </slot>
      </div>

      <div class="actions">
        <el-button
          :loading="loading"
          class="bg-indigo-400 rounded-3lg"
          type="primary"
          @click="submit"
        >
          {{ commitText }}
        </el-button>
        <el-button
          :loading="loading"
          class=" bg-orange-300 rounded-3lg text-light-100"
          type="default"
          @click="close"
        >
          取消
        </el-button>
      </div>
    </div>
  </el-drawer>
</template>

<script setup>
import { ref } from "vue";
const showDrawer = ref(false); // 抽屉开关

// 提交事件 对外暴露
const emit = defineEmits(["submit"]);
const submit = () => {
  emit("submit");
};

// loading
const loading = ref(false); // 登陆状态

const showLoading = () => (loading.value = true);
const hideLoading = () => (loading.value = false);

const props = defineProps({
  // 对外暴露属性
  title: String,
  size: {
    type: String,
    default: "45%",
  },
  destroyOnClose: {
    type: Boolean,
    default: "false",
  },
  commitText: {
    type: String,
    default: "提交",
  },
});

//打开抽屉
const open = () => {
  showDrawer.value = true;
};

// 关闭抽屉
const close = () => (showDrawer.value = false);

//向父暴露方法
defineExpose({
  open,
  close,
  showLoading,
  hideLoading,
});
</script>

<style>
.formDrawer {
  width: 100%;
  height: 100%;
  position: relative;
  @apply flex flex-col;
}

.formDrawer .body {
  flex: 1;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 50px;
  overflow: auto;
}

.formDrawer .actions {
  height: 50px;
  @apply mt-auto flex items-center;
}
</style>
